//
//  String2Class.cpp
//  Practice12
//
//  Created by 赵世杰 on 2020/8/2.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#include "String2Class.hpp"
#include <iostream>
#include <cstring>
#include <cctype>


using namespace std;


int String2::num_strings = 0;


int String2::HowMany()
{
    return num_strings;
}


String2::String2(const char *s)
{
    len = strlen(s);
    str = new char(len+1);
    strcpy(str, s);
    num_strings++;
    
}


String2::String2()
{
    len = 4;
    str = new char[1];
    str[0] = '\0';
    num_strings ++ ;
    
}


String2::String2(const String2 &st)
{
    num_strings ++;
    len = st.len;
    str = new char[len+1];
    strcpy(str, st.str);
    
}


String2::~String2()
{
    --num_strings;
    delete [] str;
    
}


String2 & String2:: operator=(const String2 &st)
{
    if(this == &st)
    {
        return *this;
    }
    
    delete [] str;
    
    len = st.len;
    
    str = new char[len+1];
    
    strcpy(str, st.str);
    
    return *this;
    
}


String2 & String2:: operator=(const char *s)
{
    delete [] str;
    
    len = strlen(s);
    
    str = new char[len+1];
    
    strcpy(str, s);
    
    return *this;
    
}


String2 & String2:: operator+(const String2 &st)
{
    
    
//    delete [] str;
    
    
    
    
    len = st.len + len;

    char *ch = new char(len+1);
    
    strcpy(ch, str);
    
    str = new char[len+1];
    
    str = strcat(ch, " and");
    
    str = strcat(str, st.str);
    
    return *this;
    
}


char & String2::operator[](int i)
{
    return str[i];
}


const char & String2::operator[](int i) const
{
    return str[i];
}


bool operator<(const String2 &st1,const String2 &st2)
{
    return strcmp(st1.str, st2.str) < 0;
}


bool operator>(const String2 &st1,const String2 &st2)
{
    return st2 < st1;
}


bool operator==(const String2 &st1,const String2 &st2)
{
    return strcmp(st1.str, st2.str) == 0;
    
}


String2 operator+(const char *s,const String2 &st1)
{
    
    String2 string;
    
    string.len = st1.len + strlen(s);
    
    string.str = new char[string.len+1];
    
    
    char *ch = new char[strlen(s)] ;
    strcpy(ch, s);
    
    string.str = strcat(ch, st1.str);
    
    return string;
    
}


ostream & operator<<(ostream &os,const String2 &st)
{
    os << st.str;
    
    return os;
    
}



istream & operator>>(istream &is,String2 &st)
{
    char temp[String2::CINLIM];
    is.get(temp,String2::CINLIM);
    
    if(is)
    {
        st = temp;
    }
    
    while (is && is.get() != '\n')
    {
        continue;
    }
    
    
    
    return is;
    
}



void String2:: Stringlow()
{
    for (int i = 0; i<strlen(str); i++)
    {
        str[i] = tolower(str[i]);
    }
    
}


void String2:: Stringbig()
{
    for (int i = 0; i<strlen(str); i++)
    {
        str[i] = toupper(str[i]);
        
    }
    
}


int String2:: StringInCharCount(char c)
{
    
    int count = 0;
    
    for (int i = 0; i<strlen(str); i++)
    {
        if(c == str[i])
        {
            count++;
        }
    }
    
    return count;
    
}





